package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.dto.OutboundAddQuery;
import com.fms.entity.*;
import com.fms.mapper.*;
import com.fms.result.ResultData;
import com.fms.service.OutboundOrderService;
import com.fms.vo.OutboundVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OutboundOrderServiceImpl extends ServiceImpl<OutboundOrderMapper, OutboundOrder> implements OutboundOrderService {

    private final OutboundOrderMapper outboundOrderMapper;

    private final WarehouseBoardMapper warehouseBoardMapper;

    private final WarehouseHardwareMapper warehouseHardwareMapper;

    private final OrdersMapper ordersMapper;

    private final SysUserMapper sysUserMapper;

    private final SysDeptMapper sysDeptMapper;

    private final OrdersHistoryMapper ordersHistoryMapper;
    //新增出库单
    @Override
    public ResultData addOutbound(OutboundAddQuery outboundAddQuery) {
        Long orderId = outboundAddQuery.getOrderId();
        //获取订单原始出库信息
        LambdaQueryWrapper<OutboundOrder> outboundOrderWrapper = new LambdaQueryWrapper<>();
        outboundOrderWrapper.eq(OutboundOrder::getOrderId,orderId);
        List<OutboundOrder> outboundOrders = outboundOrderMapper.selectList(outboundOrderWrapper);
        //数量返回
        for (OutboundOrder oldOrder : outboundOrders) {
            if (oldOrder.getMaterialType().equals("五金")){
                warehouseHardwareMapper.rollbackAmount(oldOrder.getAddHardwareAmount(), oldOrder.getMaterialId());
            }else {
                warehouseBoardMapper.rollbackAmount(oldOrder.getAddBoardAmount(), oldOrder.getMaterialId());
            }
        }
        //删除原来数据
        outboundOrderMapper.delete(outboundOrderWrapper);
        //分别添加新数据
        List<WarehouseBoard> selectedOutboundBoards = outboundAddQuery.getSelectedOutboundBoards();
        if (selectedOutboundBoards!=null){
            for (WarehouseBoard board : selectedOutboundBoards){
                OutboundOrder order = new OutboundOrder();
                order.setAddBoardAmount(board.getBoardAddAmount()).setBoardBase(board.getBoardBase())
                        .setBoardColor(board.getBoardColor()).setBoardSpecs(board.getBoardSpecs())
                        .setBoardTitle(board.getBoardTitle()+"   数量:"+board.getBoardAddAmount()).setMaterialId(board.getId()).setMaterialType("板子")
                        .setOrderId(orderId).setBoardType(board.getBoardType());
                outboundOrderMapper.insert(order);
                warehouseBoardMapper.updateWarehouseBoardOutAmount(board.getBoardAddAmount(), board.getId());
            }
        }
        List<WarehouseHardware> selectedOutboundHardware = outboundAddQuery.getSelectedOutboundHardware();
        if (selectedOutboundHardware!=null){
            for (WarehouseHardware hardware : selectedOutboundHardware){
                OutboundOrder order = new OutboundOrder();
                order.setHardwareBrand(hardware.getHardwareBrand()).setHardwareSpecial(hardware.getHardwareSpecial())
                        .setHardwareSpecs(hardware.getHardwareSpecs()).setHardwareTitle(hardware.getHardwareTitle()+"   数量:"+hardware.getHardwareAddAmount())
                        .setAddHardwareAmount(hardware.getHardwareAddAmount())
                        .setMaterialId(hardware.getId()).setMaterialType("五金").setOrderId(orderId);
                outboundOrderMapper.insert(order);
                warehouseHardwareMapper.updateWarehouseHardwareOutAmount(hardware.getHardwareAddAmount(), hardware.getId());
            }
        }
        //历史记录更新
        OrdersHistory ordersHistory = new OrdersHistory();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, outboundAddQuery.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("订单出库单修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(outboundAddQuery.getOrderId());
        ordersHistory.setOperate(0);
        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,outboundAddQuery.getOrderId())
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("出库单信息更新成功");
    }

    @Override
    public ResultData getOutbounds() {
        LambdaQueryWrapper<OutboundOrder> query = new LambdaQueryWrapper<OutboundOrder>();
        List<OutboundOrder> outboundOrders = outboundOrderMapper.selectList(query);
        List<OutboundVo> outboundVo = new ArrayList<OutboundVo>();
        for (OutboundOrder order : outboundOrders) {
            Orders orderInfo = ordersMapper.selectById(order.getOrderId());
            OutboundVo vo = new OutboundVo();
            vo.setId(order.getId()).setOrderId(orderInfo.getOrderId()).setMaterialType(order.getMaterialType())
                .setCreateDate(order.getCreateDate());
            if ("五金".equals(order.getMaterialType())){
                vo.setMaterialDetails(order.getHardwareTitle());
            }else {
                vo.setMaterialDetails(order.getBoardTitle());
            }
            outboundVo.add(vo);
        }
        return ResultData.success(outboundVo);
    }
}
